home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc A) / Acorn User China CD-ROM (UK) (Disc A).bin / DEMON / DEVELOPER / HYPERMAIL.ARC / !hypermail_c_date < prev    next >
Encoding:
Text File  |  1996-01-25  |  6.5 KB  |  257 lines

  1. /*
  2. ** Copyright (C) 1994, Enterprise Integration Technologies Corp.        
  3. ** All Rights Reserved.
  4. ** Kevin Hughes, kevinh@eit.com 
  5. ** 7/14/94
  6. */
  7.  
  8. /* Date stuff. Everything here is my own code, with the exception
  9. ** of getnweekday(), which is in the public domain. -- Kevin
  10. */
  11.  
  12. #include "hypermail.h"
  13. #include "date.h"
  14.  
  15. /*RISCOS*/
  16.  #include <time.h>
  17.  
  18. /* This converts a long date string into the form D/M/Y.
  19. */
  20.  
  21. void convtoshortdate(date, shortdate)
  22.      char *date;
  23.      char *shortdate;
  24. {
  25.         char c, d, e;
  26.         int i, month;
  27.  
  28.         c = date[4];
  29.         d = date[5];
  30.         e = date[6];
  31.  
  32.         for (i = 0; !(c == (months[i])[0] && d == (months[i])[1] &&
  33.         e == (months[i])[2]); i++)
  34.                 ;
  35.         month = i + 1;
  36.  
  37.         shortdate[0] = (month > 9) ? '1' : '0';
  38.         shortdate[1] = monthnums[month];
  39.         shortdate[2] = '/';
  40.         shortdate[3] = (date[8] == ' ') ? '0' : date[8];
  41.         shortdate[4] = date[9];
  42.         shortdate[5] = '/';
  43.         shortdate[6] = date[22];
  44.         shortdate[7] = date[23];
  45.         shortdate[8] = '\0';
  46. }
  47.  
  48. /* Splits a short date (M/D/Y) into month, day, and year components.
  49. ** It looks ugly because it should be super-fast.
  50. */
  51.  
  52. void splitshortdate(shortdate, month, day, year)
  53.      char *shortdate;
  54.      int *month;
  55.      int *day;
  56.      int *year;
  57. {
  58.         if (shortdate[0] == ' ' || shortdate[0] == '0')
  59.                 *month = (shortdate[1] - '0');
  60.         else if (shortdate[1] == '/')
  61.                 *month = (shortdate[0] - '0');
  62.         else if (shortdate[2] == '/')
  63.                 *month = (shortdate[0] - '0') * 10 + (shortdate[1] - '0');
  64.  
  65.         if (shortdate[2] =='/') {
  66.                 if (shortdate[5] == '/') {
  67.                         *day = (shortdate[3] - '0') * 10 +
  68.                         (shortdate[4] - '0');
  69.                         *year = (shortdate[6] - '0') * 10 +
  70.                         (shortdate[7] - '0');
  71.                 }
  72.                 else if (shortdate[4] == '/') {
  73.                         *day = (shortdate[3] - '0');
  74.                         *year = (shortdate[5] - '0') * 10
  75.                         + (shortdate[6] - '0');
  76.                 }
  77.         }
  78.         else if (shortdate[1] =='/') {
  79.                 if (shortdate[4] == '/') {
  80.                         *day = (shortdate[2] - '0') * 10
  81.                         + (shortdate[3] - '0');
  82.                         *year = (shortdate[5] - '0') * 10
  83.                         + (shortdate[6] - '0');
  84.                 }
  85.                 else if (shortdate[3] == '/') {
  86.                         *day = (shortdate[2] - '0');
  87.                         *year = (shortdate[4] - '0') * 10
  88.                         + (shortdate[5] - '0');
  89.                 }
  90.         }
  91. }
  92.  
  93. /* Given a short date (M/D/Y), it returns the number of seconds
  94. ** since BASEYEAR.
  95. */
  96.  
  97. long getyearsecs(shortdate)
  98.      char *shortdate;
  99. {
  100.         int i, yearday, yearsecs, prevyeardays;
  101.         int month, day, year;
  102.  
  103.         splitshortdate(shortdate, &month, &day, &year);
  104.         year += CENTURY;
  105.  
  106.         for (yearday = i = 0; i < month - 1; i++) {
  107.                 if (i == 1 && IS_LEAP(year))
  108.                         yearday++;
  109.                 yearday += monthdays[i];
  110.         }
  111.         yearday += day;
  112.  
  113.         prevyeardays = 0;
  114.         for (i = BASEYEAR; i != year; i++) {
  115.                 if (IS_LEAP(i))
  116.                         prevyeardays++;
  117.                 prevyeardays += DAYSPERYEAR;
  118.         }
  119.  
  120.         yearsecs = (yearday + prevyeardays) * SECSPERDAY;
  121.  
  122.         return yearsecs;
  123. }
  124.  
  125. /* Given a long date string, it returns the number of seconds
  126. ** since BASEYEAR.
  127. */
  128.  
  129. int convtoyearsecs(date)
  130.      char *date;
  131. {
  132.         char hourstr[3], minstr[3], secstr[3], shortdate[SHORTDATELEN];
  133.         int hours, minutes, seconds;
  134.         long yearsecs;
  135.  
  136.         convtoshortdate(date, shortdate);
  137.         yearsecs = getyearsecs(shortdate);
  138.  
  139.         sprintf(hourstr, "%c%c", date[11], date[12]);
  140.         sprintf(minstr, "%c%c", date[14], date[15]);
  141.         sprintf(secstr, "%c%c", date[17], date[18]);
  142.  
  143.         hours = atoi(hourstr);
  144.         minutes = atoi(minstr);
  145.         seconds = atoi(secstr);
  146.  
  147.         return (int) (yearsecs + (hours * SECSPERHOUR) +
  148.         (minutes * SECSPERMIN) + seconds);
  149. }
  150.  
  151. /* Gets the local time and returns it formatted.
  152. */
  153.  
  154. char *getlocaltime()
  155. {
  156.         static char s[DATESTRLEN];
  157.         time_t tp;
  158.  
  159.         time(&tp);
  160. #ifdef EURODATE
  161.         strftime(s, DATESTRLEN, "%a %d %b %Y - %H:%M:%S", localtime(&tp));
  162. #else
  163.         strftime(s, DATESTRLEN, "%a %b %d %Y - %H:%M:%S", localtime(&tp));
  164. #endif
  165.     sprintf(s, "%s %s", s, timezonestr);
  166.  
  167.         return s;
  168. }
  169.  
  170. /* Gets the local time zone.
  171. */
  172.  
  173. void gettimezone()
  174. {
  175.         time_t tp;
  176.  
  177.         time(&tp);
  178.         strftime(timezonestr, TIMEZONELEN, "%Z", localtime(&tp));
  179. }
  180.  
  181. /* Gets the current year.
  182. */
  183.  
  184. void getthisyear()
  185. {
  186.         time_t tp;
  187.  
  188.         time(&tp);
  189.         strftime(thisyear, YEARLEN, "%Y", localtime(&tp));
  190. }
  191.  
  192. /* From the number of seconds since BASEYEAR, this pretty-prints
  193. ** a date for you.
  194. */
  195.  
  196. char *getdatestr(yearsecs)
  197.      long yearsecs;
  198. {
  199.         register int day, year, month, hours, minutes;
  200.         static char date[DATESTRLEN];
  201.  
  202.         for (day = 0; yearsecs > SECSPERDAY; day++)
  203.                 yearsecs -= SECSPERDAY;
  204.  
  205.         for (year = BASEYEAR; day > DAYSPERYEAR; year++) {
  206.                 if (IS_LEAP(year))
  207.                         day--;
  208.                 day -= DAYSPERYEAR;
  209.         }
  210.  
  211.         if (IS_LEAP(year) && day > (monthdays[0] + monthdays[1])) {
  212.                 day--;
  213.                 yearsecs -= SECSPERDAY;
  214.         }
  215.  
  216.         for (month = 0; day > monthdays[month]; month++)
  217.                 day -= monthdays[month];
  218.  
  219.         for (hours = 0; yearsecs > SECSPERHOUR; hours++)
  220.                 yearsecs -= SECSPERHOUR;
  221.  
  222.         for (minutes = 0; yearsecs > SECSPERMIN; minutes++)
  223.                 yearsecs -= SECSPERMIN;
  224.  
  225. #ifdef EURODATE
  226.         sprintf(date, "%s%s %02d %d - %02d:%02d:%02d %s",
  227.         days[getnweekday(month + 1, day, year)], months[month],
  228.         day, year, hours, minutes, yearsecs, timezonestr);
  229. #else
  230.         sprintf(date, "%s%02d %s %d - %02d:%02d:%02d %s",
  231.         days[getnweekday(month + 1, day, year)], day, months[month],
  232.         year, hours, minutes, yearsecs, timezonestr);
  233. #endif
  234.  
  235.     return date;
  236. }
  237.  
  238. /* Returns the day of the year.
  239. */
  240.  
  241. int getnweekday(mn, dy, yr)
  242.      int mn;
  243.      int dy;
  244.      int yr;
  245. {
  246.         int n1, n2;
  247.  
  248.         if (mn < 3) {
  249.                 mn += 12;
  250.                 yr -= 1;
  251.         }
  252.         n1 = (26 * (mn + 1)) / 10;
  253.         n2 = (int) ((125 * (long) yr) / 100);
  254.  
  255.         return ((dy + n1 + n2 - (yr / 100) + (yr / 400) + -1) % 7);
  256. }
  257.